package com.qut.graduate.busrtquery.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qut.graduate.busrtquery.BRTQCache;
import com.qut.graduate.busrtquery.BRTQConstant;
import com.qut.graduate.busrtquery.R;
import com.qut.graduate.busrtquery.greendao.bean.StationInfoCollect;
import com.qut.graduate.busrtquery.greendao.controller.DBController;

import java.util.ArrayList;
import java.util.List;

public class SlidesStationAdapter extends RecyclerView.Adapter<SlidesLineAdapter.SlidesViewHolder> {
    private static final String TAG = "SlidesStationAdapter";
    private List<StationInfoCollect> mStationInfoCollectList = new ArrayList<>();
    private Context mContext;
    private OnDeleteListener mDeleteListener;

    public SlidesStationAdapter(Context context,List<StationInfoCollect> stationInfoCollectList) {
        mContext = context;
        mStationInfoCollectList = stationInfoCollectList;
    }

    @Override
    public SlidesLineAdapter.SlidesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_slide_layout, parent, false);
        return new SlidesLineAdapter.SlidesViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final SlidesLineAdapter.SlidesViewHolder holder, final int position) {
        holder.name.setText(mStationInfoCollectList.get(position).getName());
        holder.time.setText(mStationInfoCollectList.get(position).getTime());
        holder.deleteView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mDeleteListener!=null){
                    mDeleteListener.onDelete(position,holder.itemView);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mStationInfoCollectList!=null ? mStationInfoCollectList.size() : 0;
    }

    public void deleteItem(int position) {
        BRTQCache.getInstance().getStationInfoCollectList().remove(position);
        List<StationInfoCollect> stationInfoCollects = (ArrayList<StationInfoCollect>) DBController.getInstance().
                queryObjectByName(BRTQConstant.DB_TABLE_TYPE_STATION_INFO_COLLECT, mStationInfoCollectList.get(position).getName());
        DBController.getInstance().deleteTable(BRTQConstant.DB_TABLE_TYPE_STATION_INFO_COLLECT,
                stationInfoCollects.get(0));
        mStationInfoCollectList.remove(position);
        notifyDataSetChanged();
    }

    public void setDeleteListener(OnDeleteListener deleteListener) {
        this.mDeleteListener = deleteListener;
    }

    public interface OnDeleteListener {
        void onDelete(int position, View view);
    }

}
